/* 
 * This file is part of Last.fm Now Playing Plugi-in for Adium
 * Copyright (c) 2011 Anton Bronnikov <anton.bronnikov@gmail.com>.
 * 
 * Last.fm Now Playing Plugi-in for Adium is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation, either version 3 of the License, 
 * or (at * your option) any later version.
 * 
 * Last.fm Now Playing Plugi-in for Adium is distributed in the hope that it 
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with 
 * Last.fm Now Playing Plugi-in for Adium. If not, see 
 * <http://www.gnu.org/licenses/>.
 *
 */

#import "LastFmTrack.h"

#pragma mark -
#pragma mark Implementation : LastFmTrack

NSString * const EMPTY_STRING = @"";

@implementation LastFmTrack

#pragma mark -
#pragma mark Synthesized properties

@synthesize title  = _title;
@synthesize name   = _name;
@synthesize artist = _artist;
@synthesize album  = _album;
@synthesize url    = _url;

#pragma mark -
#pragma mark Comparison

- (BOOL) isEqualToLastFmTrack:(LastFmTrack *)anotherLastFmTrack {
  // Immediately say yes if the pointers are equal
  if (self == anotherLastFmTrack) {
    return YES;
  }
  
  return ([self.name isEqualToString:anotherLastFmTrack.name] && [self.artist isEqualToString:anotherLastFmTrack.artist]);
}

#pragma mark -
#pragma mark Aux methods

- (NSString *) description {
  return self.title;
}

#pragma mark -
#pragma mark Object life-cycle

- (id) initWithName:(NSString *)initName artist:(NSString *)initArtist album:(NSString *)initAlbum url:(NSString *)initURL {
  self = [super init];
  
  if (self) {
    _name = initName ? [initName copy] : [EMPTY_STRING copy];
    _artist = initArtist ? [initArtist copy] : [EMPTY_STRING copy];
    _album = initAlbum ? [initAlbum copy] : [EMPTY_STRING copy];
    _url = initURL ? [initURL copy] : [EMPTY_STRING copy];
    
    // Track title
    if (_name.length > 0 && _artist.length > 0) {
      _title = [[NSString stringWithFormat:@"%@ - %@", _name, _artist] retain];
    } else if (_name.length > 0) {
      _title = [_name retain];
    } else if (_artist.length > 0) {
      _title = [_artist retain];
    } else {
      _title = [EMPTY_STRING copy];
    }
  }
  
  return self;
}

- (void) dealloc {
  if (_title) {
    [_title release];
    _title = nil;
  }
  
  if (_name) {
    [_name release];
    _name = nil;
  }
  
  if (_artist) {
    [_artist release];
    _artist = nil;
  }
  
  if (_album) {
    [_album release];
    _album = nil;
  }
  
  if (_url) {
    [_url release];
    _url = nil;
  }
  
  [super dealloc];
}

@end
